package cn.wolfcode.ws;

import org.checkerframework.checker.units.qual.C;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.ConcurrentHashMap;

@ServerEndpoint("/{token}")
@Component
public class OrderWSServer {
    public static ConcurrentHashMap<String, Session> clients = new ConcurrentHashMap<>();
    @OnOpen
    public void onOpen(Session session, @PathParam("token") String token){
        System.out.println("浏览器建立连接");
        clients.put(token,session);
    }
    // 接收客户端消息
    @OnMessage
    public void onMessage(@PathParam("token")String token, String msg){
        System.out.println("客户端收到" + token + ",发送的消息:" + msg);
    }

    @OnClose
    public void onClose(@PathParam("token")String token){
        System.out.println("断开连接");
        clients.remove(token);
    }
    @OnError
    public void onError(Throwable error){error.printStackTrace();}
}
